package com.example.todo;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Spinner;

public class CreateNewTaskActivity extends Activity {
	DBAdapter myDb; //Instantiate the db

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//Don't allow the keyboard to pup up when the page loads.
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);     
		//Use the fragment UI
		setContentView(R.layout.fragment_create_new_task);
		
		//Get the task name from the intent
		Intent intent = getIntent();
		String message = intent.getStringExtra(MainActivity.EXTRA_TASK_NAME);
		EditText editText = (EditText) findViewById(R.id.task_name);
		editText.setText(message);
		
		//Open the database
        openDB();
	}

	//START NAVIGATION CODE
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	// A placeholder fragment containing a simple view.
	public static class PlaceholderFragment extends Fragment {
		public PlaceholderFragment() {
		}
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_create_new_task,
					container, false);
			return rootView;
		}
	}
	//END NAVIGATION CODE

	@Override
	protected void onDestroy() {
		super.onDestroy();
		closeDB();
	}
	private void openDB() {
		myDb = new DBAdapter(this);
		myDb.open();
	}
	private void closeDB() {
		myDb.close();
	}
	
	//Put the data in the database
	public void onClick_SaveTask(View view){
		//Task name
		EditText newTask = (EditText) findViewById(R.id.task_name);
		String stringNewTask = newTask.getText().toString();
		//Category
		Spinner newCategory = (Spinner) findViewById(R.id.add_category);
		String stringNewCategory = newCategory.getSelectedItem().toString();
		//Subject
		EditText newSubject = (EditText) findViewById(R.id.new_subject);
		String stringSubject = newSubject.getText().toString();
		//Project
		EditText newProject = (EditText) findViewById(R.id.new_project);
		String stringProject = newProject.getText().toString();
		//Due date
		EditText newDate = (EditText) findViewById(R.id.new_date);
		String stringDate = newDate.getText().toString();
		//Location
		EditText newLocation = (EditText) findViewById(R.id.new_location);
		String stringLocation = newLocation.getText().toString();
		//Description
		EditText newDescription = (EditText) findViewById(R.id.new_description);
		String stringDescription = newDescription.getText().toString();
		
		//Add the input into the database
    	long newID = myDb.insertRow(stringNewTask, stringNewCategory, stringSubject,
    			stringProject,stringDate,stringDescription,stringLocation);
    	//close the DB
    	closeDB();
    	//Close this activity and go to the main page.
    	Intent intent = new Intent(this, MainActivity.class);
    	startActivity(intent);
    }

	public void onClickClose(View view){
		//Close this activity and go to the main page.
    	Intent intent = new Intent(this, MainActivity.class);
    	startActivity(intent);
	}
}
